package com.foreverwin.jzymes.production.debur.controller;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.jzymes.common.service.ICommonService;
import com.foreverwin.jzymes.common.service.IContainerAdviceService;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.base.AjaxResult;
import com.foreverwin.jzymes.framework.event.AppEvent;
import com.foreverwin.jzymes.framework.event.AppEventPublisher;
import com.foreverwin.jzymes.framework.exception.BusinessException;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.production.debur.domain.PackLog;
import com.foreverwin.jzymes.production.debur.service.IDeburPackAdviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/production/debur")
public class DeburPackController {

    @Autowired
    IDeburPackAdviceService deburPackAdviceService;
    @Autowired
    IContainerAdviceService containerAdviceService;
    @Autowired
    ICommonService commonService;
    @Autowired
    AppEventPublisher appEventPublisher;

    /**
     * 包装
     *
     * @param packLog
     * @return
     */
    @RequestMapping( "/packItem" )
    public AjaxResult packItem( @RequestBody PackLog packLog ){
        try{
            packLog.setSite( CommonMethods.getSite() );
            deburPackAdviceService.packItem( packLog );

            /**发布事件*/
            JSONObject eventData = new JSONObject();
            eventData.put( "SITE", packLog.getSite() );
            eventData.put( "ITEM_BO", packLog.getItemBo() );
            eventData.put( "RESRCE", packLog.getResrce() );
            eventData.put( "QTY", packLog.getPackQty() );
            AppEvent<JSONObject> appEvent = new AppEvent<>( this );
            appEvent.setName( "ITEM_CHECK_OUT_EVENT" );
            appEvent.setData( eventData );
            appEventPublisher.publishEvent( appEvent );


            return AjaxResult.returnSuccess().put( "value", packLog.getContainerNumber() );
        }catch ( BusinessException e ){
            return AjaxResult.returnError( e.getMessage() );
        }
    }

    /**
     * 标签补打
     *
     * @param packLog
     * @return
     */
    @RequestMapping( "/reprint" )
    public AjaxResult reprintLabel( @RequestBody PackLog packLog ){
        try{
            packLog.setSite( CommonMethods.getSite() );
            deburPackAdviceService.reprintLabel( packLog );
            return AjaxResult.returnSuccess().put( "value", packLog.getContainerNumber() );
        }catch ( BusinessException e ){
            return AjaxResult.returnError( e.getMessage() );
        }
    }

    /**
     * 查询物料包装容器
     *
     * @param itemBo
     * @return
     */
    @RequestMapping("/findContainer")
    public AjaxResult findContainer( @RequestParam String itemBo ){
        String site =  CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ) );
        Assert.hasText( itemBo, I18nUtil.getI18nText( "common.itemNotBlank" ) );
        itemBo = commonService.findSfcItemByItem( site, itemBo );
        List<Map<String,Object>> packLevelList = containerAdviceService.findItemPackingLevel( site, itemBo );
        List<Map<String,Object>> containerList = new ArrayList<>();
        if( packLevelList != null && packLevelList.size() > 0 ){
            for( Map<String,Object> packLevelMap : packLevelList ){
                Map<String,Object> containerMap = new HashMap<>();
                containerMap.put( "container", BOHandleUtil.splitContainerHandle( (String) packLevelMap.get( "CONTAINER_BO" ) )[1] );
                containerMap.put( "maxQty", packLevelMap.get( "MAX_QTY" ) );
                containerList.add( containerMap );
            }
        }
        return AjaxResult.returnSuccess().put( "value", containerList );
    }

}

